//
//  WBTabBar.swift
//  weibo
//
//  Created by yanghong on 15/12/5.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {
    
    var composeButtonClickClosure :(()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupView()
    }
   
    required init?(coder aDecoder: NSCoder) {
       // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupView()
     
    }
   
    private func setupView() {
        
        addSubview(composeButton)
   
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        composeButton.center = CGPointMake(self.center.x, self.frame.size.height*0.5)
        
        let childWidth = self.frame.width / 5
        
        var index = 0
        
        for childView in subviews {
        
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
                var frame = childView.frame
                
                frame.size.width = childWidth
                
                frame.origin.x = CGFloat(index)*childWidth
                
                childView.frame = frame
            
                index++
                
                if index == 2 {
                
                 index++
                
                }
            
            }
        
        }
        
    }
    
    //  MARK: - composeButton 的点击事件的实现
    
  @objc private func composeButtonClick() {
    
        if let closure = self.composeButtonClickClosure {
        
         closure()
        
        }
 
    }
    
    //  MARK: - 懒加载一个composeButton 
    
    lazy var composeButton:UIButton = {
    
      let composeButton = UIButton()
        
        //给 composeButton 添加点击事件
        composeButton.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        //添加图片
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
       
        composeButton.sizeToFit()
        
    return composeButton
    }()
}
